<template>
  <view class="settle-container">
    <label class="radio">
      <radio color="#c00000" :checked="isFullCheck" @click="chooseAllGoods" /><text>全选</text>
    </label>
    <view class="amount">
      合计：
      <text style="color: #c00000;">￥{{allGoodsPrice}}</text>
    </view>
    <view class="btn-settle" @click="settlement">结算({{checkMount}})</view>
  </view>
</template>

<script>
  import {
    mapState,
    mapGetters,
    mapMutations
  } from 'vuex'

  export default {
    name: "my-settle",
    data() {
      return {
        second: 3,
        timer: null
      };
    },
    computed: {
      ...mapState('m_user', ['token']),
      ...mapGetters('m_user', ['addressStr']),
      ...mapState('m_cart', ['cart']),
      ...mapGetters('m_cart', ['checkMount', 'total', 'allGoodsPrice']),
      isFullCheck() {
        return this.total === this.checkMount
      }
    },
    methods: {
      ...mapMutations('m_cart', ['updateAllGoodsState']),
      ...mapMutations('m_user', ['updateRedirectInfo']),
      chooseAllGoods() {
        this.updateAllGoodsState(!this.isFullCheck)
      },
      settlement() {
        if (!this.addressStr) return uni.$showMsg('请添加收货地址！')

        if (!this.checkMount) return uni.$showMsg('请选择结算商品！')

        if (!this.token) return this.delayNavigate()

        this.payOrder()
      },
      delayNavigate() {
        this.second = 3
        this.showTips(this.second)

        this.timer = setInterval(() => {
          this.second--
          if (this.second <= 0) {
            clearInterval(this.timer)
            uni.switchTab({
              url: '/pages/my/my',
              success: () => {
                this.updateRedirectInfo({
                  // 跳转的方式
                  openType: 'switchTab',
                  // 从哪个页面跳转过去的
                  from: '/pages/cart/cart'
                })
              }
            })
            return
          }

          this.showTips(this.second)
        }, 1000)
      },
      showTips(n) {
        uni.showToast({
          icon: 'none',
          title: '请登录后再结算！' + n + ' 秒后自动跳转到登录页',
          mask: true,
          duration: 1500
        })
      },
      async payOrder() {
        // 创建订单
        const orderInfo = {
          // order_price: this.allGoodsPrice,
          // 开发调试的时候写死金额
          order_price: 0.01,
          consignee_addr: this.addressStr,
          goods: this.cart.filter(x => x.goods_state).map(x => ({
            goods_id: x.goods_id,
            goods_number: x.goods_count,
            goods_price: x.goods_price
          }))
        }
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/my/orders/create', orderInfo)
        if (res.meta.status !== 200) return uni.$showMsg('创建订单失败！')
        const orderNumer = res.message.order_number
      
        // 订单预支付
        const {
          data: res2
        } = await uni.$http.get('/api/public/v1/my/orders/req_unifiedorder', {
          order_number: orderNumer
        })
        if (res2.meta.status !== 200) return uni.$showMsg('预付订单生成失败！')
        const payInfo = res2.message.pay
      
        // 调用小程序API支付
        const [err, succ] = uni.requestPayment(payInfo)
        if (err) return uni.$showMsg('订单未支付！')
        // 完成了支付，进一步查询支付的结果
        const {
          data: res3
        } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', {
          order_number: orderNumber
        })
        if (res3.meta.status !== 200) return uni.$showMsg('订单未支付！')
        uni.showToast({
          title: '支付完成！',
          icon: 'success'
        })
      }
    }
  }
</script>

<style lang="scss">
  .settle-container {
    width: 100%;
    height: 50px;
    font-size: 14px;
    padding-left: 5px;
    background-color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    left: 0;
    bottom: 0;

    .radio {
      display: flex;
      align-items: center;
    }

    .btn-settle {
      min-width: 100px;
      height: 50px;
      line-height: 50px;
      text-align: center;
      background-color: #c00000;
      padding: 0 10px;
      color: white;
    }
  }
</style>
